package UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class LoginUI extends JFrame {
    private JTextField usernameField;
    private Socket socket; // Socket对象


    public LoginUI() {
        setTitle("内网通 - 登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        initUI();
    }

    private void initUI() {
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 标题
        JLabel titleLabel = new JLabel("内网通登录", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));

        JLabel userLabel = new JLabel("用户名:");
        userLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        usernameField = new JTextField();


        formPanel.add(userLabel);
        formPanel.add(usernameField);
        //窗口居中
//        setLocationRelativeTo(null);

        // 按钮
        JButton loginBtn = new JButton("登录");
        JButton cancelBtn = new JButton("取消");

        loginBtn.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        cancelBtn.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 验证登录信息
                String username = usernameField.getText();


                if (username.isEmpty() ) {
                    JOptionPane.showMessageDialog(LoginUI.this,
                            "用户名为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                //立即发送登录消息给服务器程序
                try {
                    login(username); //不能关闭管道,后续聊天界面需要管道信息

                    LoginUI.this.dispose();//关闭当前窗口

                    MainFrame mainFrame = new MainFrame(username,socket);
                    mainFrame.setVisible(true);
                } catch (IOException ex) {
                   ex.printStackTrace();
                }
                // 登录成功，打开主界面

            }

            private void login(String name) throws IOException {
                // 发送登录消息给服务器程序
                //1 建立socket管道请求与服务端socket连接
                 socket= new Socket(Constant.SERVER_IP , Constant.PORT );
                //2立即发送消息类型  1 和自己昵称
                DataOutputStream dos= new DataOutputStream(socket.getOutputStream());
                dos.writeInt(1);
                dos.writeUTF(usernameField.getText());
            }
        });

        cancelBtn.addActionListener(e -> {
            System.exit(0);
        });

        formPanel.add(loginBtn);
        formPanel.add(cancelBtn);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        add(mainPanel);
    }



}
